package com.youchuang.action.admin;

import javax.annotation.Resource;

import org.apache.log4j.Logger;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;

import com.youchuang.action.BaseAction;
import com.youchuang.bean.Category;
import com.youchuang.common.Constant;
import com.youchuang.service.AdminService;

@Namespace(value = "/admin")
@ParentPackage("shopping-default")
public class CategoryAction extends BaseAction {
	private static Logger logger = Logger.getLogger(CategoryAction.class.getSimpleName());

	private String id;
	private int turn;
	private String enName;
	private String name;
	private String description;
	private String parentId;

	@Resource
	private AdminService adminService;

	// TODO:解决分类删除的问题：方案是删除的分类的商品做标记

	@Action(value = "categorySaveOrUpdate", results = {
			@Result(name = Constant.SUCCESS, type = "json", params = { "root", "resultObj" }) })
	public String categorySaveOrUpdate() {
		logger.info("categorySaveOrUpdate is execute");
		Category category = adminService.findCategoryById(id);
		if (category == null)
			category = new Category();
		category.setTurn(turn);
		category.setEnName(enName);
		category.setName(name);
		category.setDescription(description);
		category.setParentId(parentId);
		adminService.saveOrUpdateCategory(category);
		return Constant.SUCCESS;
	}

	@Action(value = "categoryDelete", results = {
			@Result(name = Constant.SUCCESS, type = "json", params = { "root", "resultObj" }) })
	public String categoryDelete() {
		logger.info("categoryDelete is execute");
		adminService.deleteCategoryByCategoryId(id);
		return Constant.SUCCESS;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public int getTurn() {
		return turn;
	}

	public void setTurn(int turn) {
		this.turn = turn;
	}

	public String getEnName() {
		return enName;
	}

	public void setEnName(String enName) {
		this.enName = enName;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	public String getParentId() {
		return parentId;
	}

	public void setParentId(String parentId) {
		this.parentId = parentId;
	}

}
